package net.easyjava.framework.web.elastiesearch;

import net.easyjava.tools.lang.StringTools;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by wenxiaoning on 16/6/18.
 */
@Deprecated
public class ESClient {
    private  static  Client client = null;

    public static Client getClient(){
        if(client == null){
            long begin = System.currentTimeMillis();
            createClient();
            System.out.println("createClient time : " + (System.currentTimeMillis() - begin));
        }
        return client;
    }
    public static void closeClient(){
        System.out.println("ESClient.closeClient");
        if(client != null){
            client.close();
            client = null;
        }
    }

    private static synchronized void createClient(){
        if(client == null){
            String clusterName =  ESearchProperties.getInstance().getProperty("cluster.name");
            String ips =  ESearchProperties.getInstance().getProperty("ips");
            String ports =  ESearchProperties.getInstance().getProperty("ports");

            System.out.println("ESClient.createClient");
            Settings settings = Settings.settingsBuilder()
                    .put("cluster.name", clusterName)
//                    .put("client.transport.sniff", true)
                    .build();
            try {
                TransportClient transportClient = TransportClient.builder().settings(settings).build();
                String[] ipArray = ips.split(StringTools.SEPARATE_DOUHAO);
                for(int i=0;i<ipArray.length;i++){
                    String ip = ipArray[i];
                    int port = Integer.valueOf(StringTools.unSeparate(ports, i,StringTools.SEPARATE_DOUHAO));
                    if(i +1 == ipArray.length){
                        client = transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
                    }else{
                        transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
                    }
                }
                if(client == null){
                    throw new ESearchException("client can't init");
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = getClient();
        System.out.println("client.toString() = " + client.toString());
        Settings settings = client.settings();
        for(String name:settings.names()){
            System.out.println(name + "= " + settings.get(name));
        }
        System.out.println("settings.names() = " + settings.names());
        System.out.println("client.settings() = " + client.settings());
    }


}
